package com.example.cdctravel


import android.annotation.SuppressLint
import android.app.Fragment
import android.app.FragmentManager
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ListView

@SuppressLint("ValidFragment")
class NewListFragment(
    private val fManager: FragmentManager,
    private val datas: ArrayList<NewsBean>
) : Fragment(), AdapterView.OnItemClickListener {
    private var list_news: ListView? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fg_newlist, container, false)
        list_news = view.findViewById<View>(R.id.list_news) as ListView
        val myAdapter = MyAdapter2(datas, activity)
        list_news!!.adapter = myAdapter
        list_news!!.onItemClickListener = this
        return view
    }

    @SuppressLint("ResourceType")
    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
        if (Data.sta_np == true) {
            if (Data.sta_name == "admin") {
                //获取行对应数据
                val notepadBean = datas[position]
                val intent = Intent(activity, AddActivity::class.java)
                //传递数据
                intent.putExtra("id", notepadBean.id)
                intent.putExtra("time", notepadBean.title) //记录的时间
                intent.putExtra("content", notepadBean.content) //记录的内容
                activity.startActivityForResult(intent, 1)
            } else {
                val fTransaction = fManager.beginTransaction()
                val ncFragment = NewContentFragment()
                val bd = Bundle()
                bd.putString("time", datas[position].title)
                bd.putString("content", datas[position].content)
                ncFragment.arguments = bd
                //获取Activity的控件
//                TextView txt_title = (TextView) getActivity().findViewById(R.id.txt_title);
//
//                txt_title.setText(datas.get(position).getContent());
//                txt_title.setText(datas.get(position).getContent_title());

                //加上Fragment替换动画
                fTransaction.setCustomAnimations(
                    R.anim.fragment_slide_left_enter,
                    R.anim.fragment_slide_left_exit
                )
                fTransaction.replace(R.id.fl_content, ncFragment)
                //调用addToBackStack将Fragment添加到栈中
                fTransaction.addToBackStack(null)
                fTransaction.commit()
            }
        }
    }
}
